﻿using GrainManage.Data.Access;
using GrainManage.Data.Model;
using GrainManage.Data.Model.Filter;
using GrainManage.Data.Model.Table;
using GrainManage.Data.Model.View;
using GrainManage.Data.Service.Util;
using System;
using System.Collections.Generic;

namespace GrainManage.Data.Service
{
    public class TradeService : BaseService, GrainManage.Data.Service.Interfaces.ITradeService
    {
        public TradeService(IAccess proc) : base(proc) { }

        public TradeTable Get(int tradeID, string creator)
        {
            var ds = Proc.ExecuteQuery(ProcConst.Trade_GetByID, tradeID, creator);
            return ConvertToModel<TradeTable>(GetFirstRow(ds));
        }

        public List<TradeDetailView> GetDetailByContactID(int contactID, IPageable page, ICountable records, string creator)
        {
            var ds = Proc.ExecuteQuery(ProcConst.Trade_GetDetailByContactIDExtent, contactID, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<TradeDetailView>(ds, records);
        }

        /// <summary>
        /// 获取交易历史
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="contactName"></param>
        /// <param name="area"></param>
        /// <returns></returns>
        public List<TradeDetailView> SearchDetail(TradeTotalWithContactFilter filter, IPageable page, ICountable records, string creator)
        {
            var ds = Proc.ExecuteQuery(ProcConst.Trade_SearchDetail, filter.TradeType, filter.Grain, filter.ContactName,
                filter.Area, filter.StartTime, filter.EndTime, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<TradeDetailView>(ds, records);
        }

        /// <summary>
        /// 统计某种作物的交易量
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public List<TradeTotalView> GetTotal(TradeTotalFilter filter, IPageable page, ICountable records, string creator)
        {
            var ds = Proc.ExecuteQuery(ProcConst.Trade_CountTotalByGrainTimeExtent,
                filter.TradeType, filter.Grain, filter.StartTime, filter.EndTime, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<TradeTotalView>(ds, records);
        }

        /// <summary>
        /// 统计某个区域的某种作物的交易量
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="area"></param>
        /// <returns></returns>
        public List<TradeTotalWithAreaView> GetTotalByArea(TradeTotalWithAreaFilter filter, IPageable page, ICountable records, string creator)
        {
            var ds = Proc.ExecuteQuery(ProcConst.Trade_CountTotalByGrainAreaTimeExtent,
                filter.TradeType, filter.Grain, filter.Area, filter.StartTime, filter.EndTime, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<TradeTotalWithAreaView>(ds, records);
        }

        /// <summary>
        /// 统计与某人的交易次数与交易量
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="contactName"></param>
        /// <returns></returns>
        public List<TradeWithContactView> GetTotalByContactArea(TradeTotalWithContactFilter filter, IPageable page, ICountable records, string creator)
        {
            var ds = Proc.ExecuteQuery(ProcConst.Trade_CountTotalByGrainContactAreaTimeExtent,
                filter.TradeType, filter.Grain, filter.ContactName, filter.Area, filter.StartTime, filter.EndTime, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<TradeWithContactView>(ds, records);
        }

        public int Insert(TradeTable trade)
        {
            Initializor.Initialize(trade, string.Empty);
            var result = Proc.ExecuteScalar(ProcConst.Trade_Insert, trade.ContactID, trade.Grain, trade.Price, trade.Weight, trade.ActualMoney, trade.TradeType, trade.Remarks, trade.Creator);
            return result == null ? 0 : Convert.ToInt32(result);
        }

        public int Update(TradeTable trade)
        {
            Initializor.Initialize(trade, string.Empty);
            return Proc.Execute(ProcConst.Trade_UpdateByID, trade.TradeID, trade.ContactID, trade.Grain, trade.Price, trade.Weight, trade.ActualMoney, trade.TradeType, trade.Remarks, trade.Creator);
        }

        public int Delete(int priceID, string creator)
        {
            return Proc.Execute(ProcConst.Trade_DeleteByID, priceID, creator);
        }

        public List<int> Insert(List<TradeTable> trades)
        {
            var tradeIDs = new List<int>();
            foreach (var trade in trades)
            {
                tradeIDs.Add(Insert(trade));
            }
            return tradeIDs;
        }

        public List<int> Update(List<TradeTable> trades)
        {
            var invalidTradeIDs = new List<int>();
            foreach (var trade in trades)
            {
                if (Update(trade) == 0)
                {
                    invalidTradeIDs.Add(trade.TradeID);
                }
            }
            return invalidTradeIDs;
        }

        public List<int> Delete(List<int> trades, string creator)
        {
            var invalidTrades = new List<int>();
            foreach (var trade in trades)
            {
                if (Delete(trade, creator) == 0)
                {
                    invalidTrades.Add(trade);
                }
            }
            return invalidTrades;
        }



    }
}
